package com.marktotop.controller;


import com.marktotop.entity.Position;
import com.marktotop.response.ResultBean;
import com.marktotop.response.ResultCode;
import com.marktotop.service.IPositionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author marktotop
 * @since 2021-05-23
 */
@RestController
@RequestMapping("/system/basic/position")
@Api(tags = "职位信息接口")
public class PositionController {

    @Autowired
    private IPositionService positionService;


    /**
     * 获取所有职位信息
     *
     * @return
     */
    @ApiOperation(value = "获取所有职位信息")
    @RequestMapping(value = "/",method = RequestMethod.GET)
    public ResultBean getAllPosition(){
        List<Position> lists = positionService.list();
        if (null == lists){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().data("position",lists);
    }

    /**
     * 添加新的职位信息
     *
     * @param position
     * @return
     */
    @ApiOperation(value = "添加职位信息")
    @RequestMapping(value = "/",method = RequestMethod.POST)
    public ResultBean addNewPosition(@RequestBody Position position){
        // 设置当前时间
        position.setCreateDate(LocalDateTime.now());
        if (positionService.save(position)){
            return ResultBean.ok().message("职位信息添加成功！");
        }
        return ResultBean.error().message("职位信息添加失败！");
    }

    /**
     * 更新某个职位信息
     *
     * @param position
     * @return
     */
    @ApiOperation(value = "更新职位信息")
    @RequestMapping(value = "/",method = RequestMethod.PUT)
    public ResultBean updatePosition(@RequestBody Position position){
        if (positionService.updateById(position)){
            return ResultBean.ok().message("职位信息更新成功！");
        }
        return ResultBean.error().message("职位信息更新失败！");
    }

    /**
     * 删除某条职位信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除职位信息")
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public ResultBean deletePosition(@PathVariable Integer id){
        if (positionService.removeById(id)){
            return ResultBean.ok().message("职位信息删除成功！");
        }
        return ResultBean.error().message("职位信息删除失败！");
    }

    /**
     * 批量删除职位信息
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除职位信息")
    @RequestMapping(value = "/",method = RequestMethod.DELETE)
    public ResultBean deletePositionByIds(Integer[] ids){
        if (positionService.removeByIds(Arrays.asList(ids))){
            return ResultBean.ok().message("批量删除职位信息成功！");
        }
        return ResultBean.error().message("批量删除职位信息失败！");
    }


}
